﻿/*-------------------------------------------------------------------------
* 命名空间: XrayDirection.ViewModels/MainViewModel
* 类       名: MainViewModel
* 功      能:  TD
* 时      间:  2023/12/25 16:49:02
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using OpenCvSharp;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input.StylusPlugIns;
using System.Windows.Threading;
using XrayDirection.Common;
using XrayDirection.HelperManager;
using XrayDirection.Models;
using XrayDirection.Views;

namespace XrayDirection.ViewModels
{
    public class MainViewModel : BindableBase
    {

        public MainViewModel(IRegionManager regionManager)
        {
            this.regionManager = regionManager;
            menuBars = new ObservableCollection<MenuBar>();
            InitMenubars();

            NavigateCommand = new DelegateCommand<MenuBar>(DoNavigateCommand);
            HomeCommand = new DelegateCommand(DoHomeTask);

            // 更新时间的定时器
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += UpdateProduceTime;
            timer.Start();
        }

        private void DoHomeTask()
        {
            regionManager.Regions[ConstantManager.MainViewRegion].RequestNavigate(nameof(IndexView), NavCallback);
        }

        public DelegateCommand HomeCommand { get; private set; }


        private void DoNavigateCommand(MenuBar MenuBarObj)
        {
            if (MenuBarObj != null && !string.IsNullOrWhiteSpace(MenuBarObj.ViewName))
            {
                regionManager.Regions[ConstantManager.MainViewRegion].RequestNavigate(MenuBarObj.ViewName, NavCallback);
            }
        }

        /// <summary>
        /// 导航函数执行完之后的回调函数,主要用来添加操作日志这里
        /// </summary>
        /// <param name="NavResult"></param>
        private void NavCallback(NavigationResult NavResult)
        {
            if ((bool)NavResult.Result!)
            {
                NavJournal = NavResult.Context.NavigationService.Journal;
            }
        }

        private void DoGoForwardCommand()
        {
            if (NavJournal != null && NavJournal.CanGoForward)
                NavJournal.GoForward();
        }

        private void DoGoBackCommand()
        {
            if (NavJournal != null && NavJournal.CanGoBack)
                NavJournal.GoBack();
        }

        public DelegateCommand GoBackCommand { get; private set; }

        public DelegateCommand GoForwardCommand { get; private set; }

        /// <summary>
        /// 导航日志,用来进行上一步和下一步的操作
        /// </summary>
        private IRegionNavigationJournal? NavJournal { get; set; }


        // 更新显示的生产作业时间
        private void UpdateProduceTime(object? sender, EventArgs e)
        {
            DateShowBottom = DateTimeHelper.GetProductionDatetime();
        }


        public DelegateCommand<MenuBar> NavigateCommand { get; private set; }


        /// <summary>
        /// 当前选中的要显示的页面
        /// </summary>
        private MenuBar? menubarSelected;

        public MenuBar? MenubarSelected
        {
            get { return menubarSelected; }
            set
            {
                menubarSelected = value;
                RaisePropertyChanged();
            }
        }

        private string? dateShowBottom;

        public string? DateShowBottom
        {
            get { return dateShowBottom; }
            set
            {
                dateShowBottom = value;
                RaisePropertyChanged();
            }
        }




        private string? appMaxminzeState;

        public string? AppMaxminzeState
        {
            get { return appMaxminzeState; }
            set
            {
                appMaxminzeState = value;
                RaisePropertyChanged();
            }
        }

        private ObservableCollection<MenuBar> menuBars = null!;
        private readonly IRegionManager regionManager;

        public ObservableCollection<MenuBar> MenuBars
        {
            get { return menuBars; }
            set
            {
                menuBars = value;
                RaisePropertyChanged();
            }
        }
        private void InitMenubars()
        {
            menuBars.Clear();
            menuBars.Add(new MenuBar() { Icon = "Home", Title = "生产作业", ViewName = "IndexView" });
            menuBars.Add(new MenuBar() { Icon = "History", Title = "生产记录", ViewName = "HistoryView" });
            menuBars.Add(new MenuBar() { Icon = "Camera", Title = "工具", ViewName = "ToolView" });
            menuBars.Add(new MenuBar() { Icon = "Cog", Title = "设置", ViewName = "SettingsView" });
            MenubarSelected = menuBars.FirstOrDefault();
        }

    }
}
